package com.safe.text;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class StoredData {
	// Declaration of SMS Table Variables
	public static final String SMS_ID = "sms_id";
	public static final String SMS_DATESENT = "sms_datesent";
	public static final String SMS_TIMESENT = "sms_timesent";
	public static final String SMS_MPH = "sms_mph";
	public static final String SMS_KMPH = "sms_kmph";
	private static final String SMS_TABLE = "SMS_Events";

	// Declaration of User Data Variables
	public static final String USER_ID = "u_id";
	public static final String USER_PASSWORD = "u_password";
	public static final String USER_PHONE = "u_phone";
	public static final String USER_EMAIL = "u_email";
	private static final String USER_TABLE = "User_Date";

	private static final String TAG = "StoredData";
	private static final String DATABASE_NAME = "StoredData";
	private static final int DATABASE_VERSION = 2;

	private static final String DATABASE_CREATE_SMS_TABLE = "create table "
			+ SMS_TABLE + " (" + SMS_ID
			+ " integer primary key autoincrement, " + SMS_DATESENT
			+ " text not null, " + SMS_TIMESENT + " text not null, " + SMS_MPH
			+ " text not null, " + SMS_KMPH + " text not null);";
	private static final String DATABASE_CREATE_USER_TABLE = "create table "
			+ USER_TABLE + " (" + USER_ID
			+ " integer primary key autoincrement, " + USER_PASSWORD
			+ " text not null, " + USER_PHONE + " text not null, " + USER_EMAIL
			+ " text not null);";

	private final Context context;

	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;

	public StoredData(Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);

	}

	private static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE_SMS_TABLE);
			db.execSQL(DATABASE_CREATE_USER_TABLE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS " + SMS_TABLE);
			db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE);
			onCreate(db);
		}
	}

	// ---opens the database---
	public StoredData open() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}

	// ---closes the database---
	public void close() {
		DBHelper.close();
	}

	public Cursor query(String query) {
		db.execSQL(query);
		return null;
	}

	// ---insert a location into the database---
	public long insertSms(String sms_datesent, String sms_timesent,
			String sms_mph, String sms_kmph) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(SMS_DATESENT, sms_datesent);
		initialValues.put(SMS_TIMESENT, sms_timesent);
		initialValues.put(SMS_MPH, sms_mph);
		initialValues.put(SMS_KMPH, sms_kmph);
		return db.insert(SMS_TABLE, null, initialValues);
	}

	// ---insert a message event into the database---
	public long insertUser(String u_password, String u_phone, String u_email) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(USER_PASSWORD, u_password);
		initialValues.put(USER_PHONE, u_phone);
		initialValues.put(USER_EMAIL, u_email);
		return db.insert(USER_TABLE, null, initialValues);
	}

	// ---deletes a location---
	public boolean deleteSMS(long l_id) {

		return db.delete(SMS_TABLE, SMS_ID + "=" + l_id, null) > 0;
	}

	// ---deletes a message---
	public boolean deleteUser(long m_id) {

		return db.delete(USER_TABLE, USER_ID + "=" + m_id, null) > 0;
	}

	// ---deletes all records of the Location Table
	public void deleteAllSMS() {
		this.db.delete(SMS_TABLE, null, null);
	}

	// ---deletes all records of the Message Table
	public void deleteAllUsers() {
		this.db.delete(USER_TABLE, null, null);
	}

	// ---retrieves all the locations---
	public Cursor getAllSMS() {
		return db.query(SMS_TABLE, new String[] { SMS_ID, SMS_DATESENT,
				SMS_TIMESENT, SMS_MPH, SMS_KMPH }, null, null, null, null,
				null, null);
	}

	// ---retrieves all the messages---
	public Cursor getAllUsers() {
		return db.query(USER_TABLE, new String[] { USER_ID, USER_PASSWORD,
				USER_PHONE, USER_EMAIL }, null, null, null, null, null, null);
	}

	// ---retrieves a particular location---
	public Cursor getSMS(long sms_id) throws SQLException {
		Cursor mCursor = db.query(true, SMS_TABLE, new String[] { SMS_ID,
				SMS_DATESENT, SMS_TIMESENT, SMS_MPH, SMS_KMPH }, SMS_ID + "="
				+ sms_id, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	// ---retrieves a particular message---
	public Cursor getUser(long u_id) throws SQLException {
		Cursor mCursor = db.query(true, USER_TABLE, new String[] { USER_ID,
				USER_PASSWORD, USER_PHONE, USER_EMAIL }, USER_ID + "=" + u_id,
				null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	// ---updates a location---
	public boolean updateSMS(long sms_id, String sms_datesent,
			String sms_timesent, String sms_mph, String sms_kmph) {
		ContentValues args = new ContentValues();
		args.put(SMS_DATESENT, sms_datesent);
		args.put(SMS_TIMESENT, sms_timesent);
		args.put(SMS_MPH, sms_mph);
		args.put(SMS_KMPH, sms_kmph);
		return db.update(SMS_TABLE, args, SMS_ID + "=" + sms_id, null) > 0;
	}

	// ---updates a message---
	public boolean updateUser(long u_id, String u_password, String u_phone,
			String u_email) {
		ContentValues args = new ContentValues();
		args.put(USER_PASSWORD, u_password);
		args.put(USER_PHONE, u_phone);
		args.put(USER_EMAIL, u_email);
		return db.update(USER_TABLE, args, USER_ID + "=" + u_id, null) > 0;
	}
}